package com.tjshop.android;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

/**
 * Created by gytaiSSD on 2014/12/26.
 */
public class FragmentManageHelper extends FragmentActivity {

	/**
	 * Fragment跳转
	 * 
	 * @param fromFragmentClass
	 * @param toFragmentClass
	 * @param args
	 */
	public void turnToFragment(Class<? extends Fragment> fromFragmentClass,
			Class<? extends Fragment> toFragmentClass, Bundle args) {

		FragmentManager fm = getSupportFragmentManager();
		// 被切换的Fragment标签
		String fromTag = fromFragmentClass.getSimpleName();
		// 切换到的Fragment标签
		String toTag = toFragmentClass.getSimpleName();
		// 查找切换的Fragment
		Fragment fromFragment = fm.findFragmentByTag(fromTag);
		Fragment toFragment = fm.findFragmentByTag(toTag);
		// 如果要切换到的Fragment不存在，则创建
		if (toFragment == null) {
			try {
				toFragment = toFragmentClass.newInstance();
				toFragment.setArguments(args);
			} catch (java.lang.InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		// 如果有参数传递，
		if (args != null && !args.isEmpty()) {
			toFragment.getArguments().putAll(args);
		}
		// Fragment事务
		FragmentTransaction ft = fm.beginTransaction();
		// 设置Fragment切换效果
		ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out,
				android.R.anim.fade_in, android.R.anim.fade_out);
		/**
		 * 如果要切换到的Fragment没有被Fragment事务添加，则隐藏被切换的Fragment，添加要切换的Fragment
		 * 否则，则隐藏被切换的Fragment，显示要切换的Fragment
		 */
		ft.hide(fromFragment).show(toFragment);
		// 添加到返回堆栈
		// ft.addToBackStack(tag);
		// 不保留状态提交事务
		ft.commitAllowingStateLoss();
	}
}
